// 选择时间和日期
$(".form-date").datetimepicker({
    weekStart: 1,
    todayBtn:  1,
    autoclose: 1,
    todayHighlight: 1,
    startView: 2,
    forceParse: 0,
    showMeridian: 1,
    format: "yyyy-mm-dd hh:ii:ss"
});
// 打开模态框显示文本编辑器
var editor = "";
$('#addModal').on('show.zui.modal', function () {
    /*KindEditor*/
    editor = KindEditor.create('#content', {
        resizeMode : 0,
        width : '100%',
        height : '5%',
        allowPreviewEmoticons : false,
        allowImageUpload : false,
        items : [
            'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
            'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
            'insertunorderedlist', '|', 'emoticons', 'image', 'link']
        ,
        // 失去焦点时，将上传内容同步到textarea中
        afterBlur: function () {
            this.sync();
        }
    });
});
// 关闭模态框 bootstrapvalidator实现校验、校验清除重置
$('#addModal').on('hidden.zui.modal', function () {
    // 移除上次的校验配置
    $("#addForm").data('bootstrapValidator').destroy();
    $('#addForm').data('bootstrapValidator',null);
    // 重新添加校验
    formValidator();
});
// 加载收件人
function optUsers() {
    // 加载所有的系统用户列表
    $.ajax({
        type : "GET",
        url : "/admin/user/tree",
        contentType: "application/json;charset=utf-8",
        dataType:"json",
        success : function (result) {
            if (result.errorCode == 'y') {
                // 初始化数据表格2
                var bg = $('#datagridExample').datagrid({
                    dataSource: {
                        cols:[
                            {name: 'time', label: '时间', width: 132},
                            {name: 'hera', label: '英雄', width: 134},
                            {name: 'action', label: '动作', width: 109},
                            {name: 'target', label: '目标', width: 109},
                            {name: 'desc', label: '描述', width: 287}
                        ],
                        array:[
                            {time: '00:11:12', hero:'幻影刺客', action: '击杀', target: '斧王', desc: '幻影刺客击杀了斧王。'},
                            {time: '00:13:22', hero:'幻影刺客', action: '购买了', target: '隐刀', desc: '幻影刺客购买了隐刀。'},
                            {time: '00:19:36', hero:'斧王', action: '购买了', target: '黑皇杖', desc: '斧王购买了黑皇杖。'},
                            {time: '00:21:43', hero:'力丸', action: '购买了', target: '隐刀', desc: '力丸购买了隐刀。'}
                        ]
                    },
                });
                $("#users").html(bg);
                // 打开加载收件人列表模态框
                $('#usersModal').modal({
                    keyboard : false,
                    show     : true
                });
            } else {
                new $.zui.Messager('提示消息：' + result.errorText, {
                    icon: 'exclamation-sign',
                    type: 'success',
                    time: 2000
                }).show();
            }
        },error:function(){
            new $.zui.Messager('提示消息：' + "系统异常", {
                icon: 'exclamation-sign',
                type: 'success',
                time: 2000
            }).show();
        }
    });
}
// 抽取初始化表单验证
function formValidator() {
    $('#addForm').bootstrapValidator({
        //关键配置，表示只对于禁用域不进行验证，其他的表单元素都要验证
        /*excluded:[":disabled"],*/
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            users: {
                validators: {
                    notEmpty: {
                        message: '收件人不能为空'
                    }
                }
            },
            type: {
                validators: {
                    notEmpty: {
                        message: '类型不能为空'
                    }
                }
            },
            title: {
                validators: {
                    notEmpty: {
                        message: '标题不能为空'
                    }
                }
            },
            content : {
                validators: {
                    notEmpty: {
                        message: 'The content is required and cannot be empty'
                    },
                    callback: {
                        message: 'The content must be less than 200 characters long',
                        callback: function(value, validator, $field) {
                            // Get the plain text without HTML
                            var div  = $('<div/>').html(value).get(0),
                                text = div.textContent || div.innerText;

                            return text.length <= 200;
                        }
                    }
                }
            },
            level: {
                validators: {
                    notEmpty: {
                        message: '级别不能为空'
                    }
                }
            }
        }
    });
}
// 保存草稿、发送
$(document).ready(function() {
    formValidator();
    $form = $('#addForm');
    //保存草稿 非submit按钮点击后进行验证，如果是submit则无需此句直接验证
    $("#save").click(function () {
        $form.bootstrapValidator('validate');//提交验证
        if ($form.data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
            // 发送 保存草稿 ajax请求
            $.post("/admin/msgSend/save", $form.serialize(), function(result) {
                if (result.errorCode == 'y') {
                    new $.zui.Messager('提示消息：' + result.errorText, {
                        icon: 'exclamation-sign',
                        type: 'success',
                        time: 2000
                    }).show();
                    setTimeout("location.reload()", 1000);
                } else {
                    new $.zui.Messager('提示消息：' + result.errorText, {
                        icon: 'exclamation-sign',
                        type: 'danger',
                        time: 2000
                    }).show();
                }
            }, 'json');
        }
    });

    //发送 非submit按钮点击后进行验证，如果是submit则无需此句直接验证
    $("#send").click(function () {
        $form.bootstrapValidator('validate');//提交验证
        if ($form.data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
            // 发送 发送消息 ajax请求
            $.post("/admin/msgSend/send", $form.serialize(), function(result) {
                if (result.errorCode == 'y') {
                    new $.zui.Messager('提示消息：' + result.errorText, {
                        icon: 'exclamation-sign',
                        type: 'success',
                        time: 2000
                    }).show();
                    setTimeout("location.reload()", 1000);
                } else {
                    new $.zui.Messager('提示消息：' + result.errorText, {
                        icon: 'exclamation-sign',
                        type: 'danger',
                        time: 2000
                    }).show();
                }
            }, 'json');
        }
    });

    // 修改表单验证
    $('#editForm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            title: {
                validators: {
                    notEmpty: {
                        message: '博客标题不能为空'
                    }
                }
            },
            content: {
                validators: {
                    notEmpty: {
                        message: '博客内容不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();
        // Get the form instance
        var $form = $(e.target);
        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');
        // Use Ajax to submit form data
        $.post($form.attr('action'), $form.serialize(), function(result) {
            if (result.errorCode == 'y') {
                new $.zui.Messager('提示消息：' + result.errorText, {
                    icon: 'exclamation-sign',
                    type: 'success',
                    time: 2000
                }).show();
                setTimeout("location.reload()", 1000);
            } else {
                new $.zui.Messager('提示消息：' + result.errorText, {
                    icon: 'exclamation-sign',
                    type: 'success',
                    time: 2000
                }).show();
            }
        }, 'json');
    });
});
// 发送
function sendNode(id) {
    new $.zui.Messager('提示消息：' + "发送成功", {
        icon: 'exclamation-sign',
        type: 'danger',
        time: 2000
    }).show();
}
// 编辑
function editNode(id) {
    bootbox.confirm({
        title: "操作提示",
        size: "small",
        message: "你确定要编辑选择的数据吗？",
        callback: function (result) {
            if (result) {
                $.ajax({
                    type : "GET",
                    cache : false,
                    url : "/admin/role/get/" + id,
                    contentType: "application/json;charset=utf-8",
                    dataType:"json",
                    success : function (result) {
                        if (result.errorCode == 'y') {
                            $("#id").val(result.roleDto.id);
                            $("#name").val(result.roleDto.name);
                            $("#code").val(result.roleDto.code);
                            $('#editModal').modal({
                                keyboard : false,
                                show     : true
                            });
                        } else {
                            new $.zui.Messager('提示消息：' + result.errorText, {
                                icon: 'exclamation-sign',
                                type: 'danger',
                                time: 2000
                            }).show();
                        }
                    },error:function(){
                        new $.zui.Messager('提示消息：' + "系统异常", {
                            icon: 'exclamation-sign',
                            type: 'danger',
                            time: 2000
                        }).show();
                    }
                });
            }
        }
    });
}
// 删除
function deleteNode(id) {
    // 定义一个存放id集合的数组
    bootbox.confirm({
        title: "操作提示",
        size: "small",
        message: "你确定要删除选择的数据吗？",
        callback: function (result) {
            if (result) {
                $.ajax({
                    type : "DELETE",
                    cache : false,
                    url : "/admin/role/delete/" + id,
                    contentType: "application/x-www-form-urlencoded;charset=utf-8",
                    dataType:"json",
                    success: function (result) {
                        if (result.errorCode == 'y') {
                            new $.zui.Messager('提示消息：' + result.errorText, {
                                icon: 'exclamation-sign',
                                type: 'success',
                                time: 2000
                            }).show();
                            // 一秒后跳转
                            setTimeout("location.reload()", 1000);
                        } else {
                            new $.zui.Messager('提示消息：' + result.errorText, {
                                icon: 'exclamation-sign',
                                type: 'danger',
                                time: 2000
                            }).show();
                        }
                    },error : function(result) {
                        new $.zui.Messager('提示消息：' + result.errorText, {
                            icon: 'exclamation-sign',
                            type: 'danger',
                            time: 2000
                        }).show();
                    }
                });
            }
        }
    });
}
// 下拉框
$('select.chosen-select').chosen({
    no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
    disable_search_threshold: 10, // 10 个以下的选择项则不显示检索框
    search_contains: true         // 从任意位置开始检索
});
// 分页
function queryAll(pageNum, pageSize){
    var status = $(".form-control").find("option:selected").val();
    var searchParam = $("input[name='searchParam']").val();
    var startDate = $("input[name='startDate']").val();
    var endDate = $("input[name='endDate']").val();
    document.location.href="/admin/msgSend/list?pageNum="+pageNum+"&status="+status+"&searchParam="+searchParam+"&startDate="+startDate+"&endDate="+endDate;
}